#include "WFT0375_spi.h"
#include "WFT0375_segment.h"
#include <time.h>

/*int num_1[14] = {14,7,  12,4,   11,7,   13,5,   15,2,   15,1,   14,5};
 int num_2[14] = {13,2,  12,6,   9,2,    10,4,   10,3,   12,5,   10,5};
 int num_3[14] = {13,1,  11,0,   9,5,    5,4,    9,1,    12,7,   10,7};
 int num_4[14] = {13,0,  11,2,   7,0,    5,3,    9,6,    11,1,   10,6};
 int num_5[14] = {15,7,  14,1,   14,0,   0,1,    15,4,   15,5,   15,6};
 int num_6[14] = {13,3,  13,7,   13,6,   0,0,    14,2,   14,4,   14,3};
 int num_7[14] = {11,6,  11,5,   12,2,   1,7,    12,0,   12,1,   12,3};
 int num_8[14] = {8,1,   8,5,    8,6,    8,3,    9,4,    9,3,    9,7};
 int num_9[14] = {6,3,   6,6,    6,7,    6,0,    7,5,    7,4,    6,5};
 int num_10[14] ={4,2,   4,3,    1,4,    1,5,    1,6,    4,1,    3,6};
 int num_11[14] ={4,5,   4,6,    1,1,    1,2,    1,3,    4,4,    4,5};
 int num_12[14] ={3,3,   3,5,    2,5,    2,6,    2,7,    4,7,    2,0};
 int num_13[14] ={7,6,   6,1,    5,0,    5,2,    5,1,    6,2,    6,4};*/

static int num_segments[13][14] = {
		{ 14, 7, 12, 4, 11, 7, 13, 5, 15, 2, 15, 1, 14, 5 },
		{ 13, 2, 12, 6, 9, 2, 10, 4, 10, 3, 12, 5, 10, 5 },
		{ 13, 1, 11, 0, 9, 5, 5, 4, 9, 1, 12, 7, 10, 7 },
		{ 13, 0, 11, 2, 7, 0, 5, 3, 9, 6, 11, 1, 10, 6 },
		{ 15, 7, 14, 1, 14, 0, 0, 1, 15, 4, 15, 5, 15, 6 },
		{ 13, 3, 13, 7, 13, 6, 0, 0, 14, 2, 14, 4, 14, 3 },
		{ 11, 6, 11, 5, 12, 2, 1, 7, 12, 0, 12, 1, 12, 3 },
		{ 8, 1, 8, 5, 8, 6, 8, 3, 9, 4, 9, 3, 9, 7 },
		{ 6, 3, 6, 6, 6, 7, 6, 0, 7, 5, 7, 4, 6, 5 },
		{ 4, 2, 4, 3, 1, 4, 1, 5, 1, 6, 4, 1, 3, 6 },
		{ 4, 5, 4, 6, 1, 1, 1, 2, 1, 3, 4, 4, 2, 1 },
		{ 3, 3, 3, 5, 2, 5, 2, 6, 2, 7, 4, 7, 2, 0 },
		{ 7, 6, 6, 1, 5, 0, 5, 2, 5, 1, 6, 2, 6, 4 },
};

static int digits[16][7] = {
		{ 1, 2, 3, 4, 5, 6, 0 },  // 0
		{ 2, 3, 0, 0, 0, 0, 0 },  // 1
		{ 1, 2, 4, 5, 7, 0, 0 },  // 2
		{ 1, 2, 3, 4, 7, 0, 0 },  // 3
		{ 2, 3, 6, 7, 0, 0, 0 },  // 4
		{ 1, 3, 4, 6, 7, 0, 0 },  // 5
		{ 1, 3, 4, 5, 6, 7, 0 },  // 6
		{ 1, 2, 3, 0, 0, 0, 0 },  // 7
		{ 1, 2, 3, 4, 5, 6, 7 },  // 8
		{ 1, 2, 3, 4, 6, 7, 0 },  // 9
		{ 1, 2, 3, 5, 6, 7, 0 },  // A
		{ 3, 4, 5, 6, 7, 0, 0 },  // b
		{ 1, 4, 5, 6, 0, 0, 0 },  // c
		{ 2, 3, 4, 5, 7, 0, 0 },  // d
		{ 1, 4, 5, 6, 7, 0, 0 },  // E
		{ 1, 5, 6, 7, 0, 0, 0 }   // F
};

void set_segment(uint8_t *displayData, uint8_t segment_byte, uint8_t segment_bit, uint8_t value)
{
	// depending on whether the display is inverted and the desired value
	// the bit needs to be set or cleared
	if (value == 1) {
		displayData[segment_byte] |= (1 << segment_bit);
	}
	else {
		displayData[segment_byte] &= ~(1 << segment_bit);
	}
}

void clear_digit(uint8_t *displayData, uint8_t where)
{

	int segment_byte;
	int segment_bit;
	for (int i = 0; i < 7; i++)
			{
		// get the segment needed to display the digit 'digit',
		// this is stored in the array 'digits'
		int segment = digits[8][i] - 1;
		// segment = -1 indicates that there are no more segments to display
		if (segment >= 0)
				{
			segment_byte = num_segments[where - 1][2 * segment];
			segment_bit = num_segments[where - 1][1 + 2 * segment];
			set_segment(displayData, segment_byte, segment_bit, 0);
		}
		else
			// there are no more segments to be displayed
			break;
	}
}

void set_digit(uint8_t *displayData, uint8_t where, uint8_t digit)
{
	// check if the input is valid
	if ((digit >= 0) && (digit < 16) && (where >= 1) && (where <= 13))
			{
		clear_digit(displayData, where);
		// set the segments, there are up to 11 segments in a digit
		int segment_byte;
		int segment_bit;
		for (int i = 0; i < 7; i++)
				{
			// get the segment needed to display the digit 'digit',
			// this is stored in the array 'digits'
			int segment = digits[digit][i] - 1;
			// segment = -1 indicates that there are no more segments to display
			if (segment >= 0)
					{
				segment_byte = num_segments[where - 1][2 * segment];
				segment_bit = num_segments[where - 1][1 + 2 * segment];
				set_segment(displayData, segment_byte, segment_bit, 1);
			}
			else
				// there are no more segments to be displayed
				break;
		}
	}
}

void displayHumidity(uint8_t *displayData, uint8_t humidity, uint8_t mainFlag) {
	if (mainFlag == TRUE) {
		for (int i = 1; i < 5; i++) {
			clear_digit(displayData, i);
		}
		for (int i = 8; i < 10; i++) {
					clear_digit(displayData, i);
				}
		clear_digit(displayData, 13);
		set_segment(displayData, 11, 4, 0);
		set_segment(displayData, 9, 0, 0);
		set_segment(displayData, 5, 5, 0);
		set_segment(displayData, 7, 7, 0);

		set_digit(displayData, 3, humidity / 10);
		set_digit(displayData, 4, humidity % 10);
		set_segment(displayData, 7, 2, 1);
	}
	else {
		set_digit(displayData, 8, humidity / 10);
		set_digit(displayData, 9, humidity % 10);
		set_segment(displayData, 5, 6, 1);
	}

}

void displayTemp(uint8_t *displayData, uint16_t temperature, uint8_t unit, uint8_t mainFlag) {
	if (mainFlag == TRUE) {
		for (int i = 1; i < 5; i++) {
			clear_digit(displayData, i);
		}
		for (int i = 10; i < 13; i++) {
			clear_digit(displayData, i);
		}
		set_segment(displayData, 2, 4, 0);
		set_segment(displayData, 3, 2, 0);
		set_segment(displayData, 11, 4, 0);
		set_segment(displayData, 9, 0, 0);
		set_segment(displayData, 7, 2, 0);
		set_segment(displayData, 1, 0, 0);

		set_digit(displayData, 1, temperature / 100);
		set_digit(displayData, 2, (temperature / 10) % 10);
		set_segment(displayData, 5, 5, 1);
		set_digit(displayData, 3, temperature % 10);
		if (unit == CELSIUS) {
			set_segment(displayData, 7, 7, 1);
			set_digit(displayData, 13, 0x0c);
		}
		else if (unit == FAHRENHEIT) {
			set_segment(displayData, 7, 7, 1);
			set_digit(displayData, 13, 0x0f);
		}
	}
	else {
		set_digit(displayData, 10, temperature / 100);
		set_digit(displayData, 11, (temperature / 10) % 10);
		set_segment(displayData, 1, 0, 1);
		set_digit(displayData, 12, temperature % 10);
		if (unit == CELSIUS) {
			set_segment(displayData, 2, 4, 1);
		}
		else if (unit == FAHRENHEIT) {
			set_segment(displayData, 3, 2, 1);
		}
	}

}

void displayBattery(uint8_t *displayData, uint8_t batteryStatus) {
	if (batteryStatus == TRUE) {
		set_segment(displayData, 14, 6, 1);
		set_segment(displayData, 13, 4, 1);
	}
	else {
		set_segment(displayData, 14, 6, 1);
		set_segment(displayData, 13, 4, 0);
	}
}

void displayBLE(uint8_t *displayData, uint8_t bleStatus) {
	if (bleStatus == TRUE) {
		set_segment(displayData, 11, 3, 1);
	}
	else {
		set_segment(displayData, 11, 3, 0);
	}
}

void displayEmoji(uint8_t *displayData, uint8_t emoji) {
	switch (emoji)
	{
	case E_SMILE:
		set_segment(displayData, 2, 2, 1);
		set_segment(displayData, 3, 1, 1);
		set_segment(displayData, 3, 0, 1);
		set_segment(displayData, 2, 3, 1);
		set_segment(displayData, 3, 4, 0);
		set_segment(displayData, 3, 7, 0);
		break;
	case E_SAD:
		set_segment(displayData, 2, 2, 1);
		set_segment(displayData, 3, 1, 0);
		set_segment(displayData, 3, 0, 0);
		set_segment(displayData, 2, 3, 0);
		set_segment(displayData, 3, 4, 1);
		set_segment(displayData, 3, 7, 1);
		break;
	}
}

void displayTime(uint8_t *displayData, uint8_t hour, uint8_t miniute) {
	set_digit(displayData, 1, hour / 10);
	set_digit(displayData, 2, hour % 10);
	set_segment(displayData, 11, 4, 1);
	set_segment(displayData, 9, 0, 1);
	set_digit(displayData, 3, miniute / 10);
	set_digit(displayData, 4, miniute % 10);
}
